﻿using System;
using System.Collections.Generic;
using System.Text;

namespace CommonUtils
{
    /// <summary>
    /// 缓存工具
    /// </summary>
    public static class CacheUtil
    {
        private static List<string> _accessedKeys = new List<string>();

        public static bool IsFirstAccess(string key)
        {
            if (_accessedKeys.Contains(key))
                return false;
            _accessedKeys.Add(key);
            return true;
        }

        #region 短存储

        /*
         * var cache = new MemoryCache(Options.Create(new MemoryCacheOptions()));
         * cache.Set("hello", StringIdentifiers.ChinaInnovation);
         * cache.Get("hello").Print();
         * 框架自带方法，这边暂不使用
         * 无法转为长存储 设置过期时间ExpirationScanFrequency无效 程序结束时释放
         */

        private static StringObjectMap _cache { get; } = new StringObjectMap();

        public static void Set(string key, object value)
        => _cache.Set(key, value);

        public static void Save(string key, object value)
        => Set(key, value);

        public static TValue Get<TValue>(string key, TValue defaultValue = default)
        => (TValue)_cache.Get(key, defaultValue, false);

        public static string GetString(string key)
        => Get<string>(key);

        public static string Get(string key)
        => GetString(key);

        public static bool Contains(string key)
        => _cache.ContainsKey(key);

        public static bool Contains(string key, out string value)
        {
            value = Get(key);
            return Contains(key);
        }
        #endregion

        #region 长存储
        private static StringObjectMap _longCache { get; } = new StringObjectMap();

        public static string SaveFolder = PathConfig.AppDataFolder.Combine("cache");

        private static string GetSavePath(string key)
        => SaveFolder.Combine(key + ".json");

        public static void SetWithFile(string key, object value)
        {
            _longCache.Set(key, value);
            FileUtil.Save(GetSavePath(key), value.ToJson(true));
        }

        public static void SaveWithFile(string key, object value)
        => SetWithFile(key, value);

        public static TValue GetFromFile<TValue>(string key, TValue defaultValue = default)
        {
            if (_longCache.ContainsKey(key))
                return (TValue)_longCache[key];

            var path = GetSavePath(key);
            if (FileUtil.Exists(path))
                return JsonUtil.DeserializeFromFile(path, defaultValue);

            return defaultValue;
        }

        public static string GetStringFromFile(string key)
        => GetFromFile<string>(key);

        public static string GetFromFile(string key)
        => GetStringFromFile(key);

        public static bool ContainsInFiles(string key)
        => _cache.ContainsKey(key) || FileUtil.Exists(GetSavePath(key));

        public static bool ContainsInFiles(string key, out string value)
        {
            value = GetFromFile(key);
            return ContainsInFiles(key);
        }
        #endregion
    }
}
